package com.acme.bankapp.domain.bank;

import java.util.Date;

public class Bank {
    private static int MAX_CLIENTS = 10;
    private Client[] clients= new Client[MAX_CLIENTS];
    
    public class DebugListener implements ClientRegistrationListener {
        public DebugListener() {
        }

        @Override
        public void onClientAdded(Client client) {
        	Date dte = new Date();
            System.out.println(dte.toString()+ " " + client.getName());
        }
    }
    
    public class PrintClientListener implements ClientRegistrationListener {
        public PrintClientListener() {
        }

        @Override
        public void onClientAdded(Client client) {
            System.out.println(client.getName());
        }
    }
    
    public class EmailNotificationListener implements
            ClientRegistrationListener {
        public EmailNotificationListener() {
        }

        @Override
        public void onClientAdded(Client client) {
            System.out.println("Notification email for client " + client.getName() + " to be sent.");
        }
    }

    ClientRegistrationListener[] listeners = new ClientRegistrationListener[]{
    	new PrintClientListener(),
    	new EmailNotificationListener(),
    	new DebugListener()
    };

    public Bank(Client[] clients){
    	for (int i = 0; i < clients.length; i++) {
   				this.clients[i] = clients[i];
        }
    }
    
    public Bank(ClientRegistrationListener[] listeners, Client[] clients) {
    	for (int i = 0; i < clients.length; i++) {
    		this.clients[i] = clients[i];
    		for (ClientRegistrationListener listener : listeners) {
    			listener.onClientAdded(clients[i]);
    		}
    	}
    }

    public void addClient(Client newClient) {
    	for (int i = 0; i < MAX_CLIENTS; i++) {
    		if (clients[i] == null) {
   				clients[i] = newClient;
               	break;
            }
        }
        for (ClientRegistrationListener listener : listeners) {
            listener.onClientAdded(newClient);
        }
    	
    }
    public Client getClient(String clientName) {
    	Client client_retu = null;
       	for (int i = 0; i < MAX_CLIENTS; i++) {
           		if (clients[i].getName().equals(clientName)) {
       			client_retu = clients[i];
       			break;
       		}
       	}
       	return client_retu;
    }
    public Client[] getClients() {
    	return clients;
    }
}
